package com.mkvld.appietijdenv2.fragments;

import java.util.List;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.activities.ActMainV9;
import com.mkvld.appietijdenv2.data.DayShifts;
import com.mkvld.appietijdenv2.data.Holiday;
import com.mkvld.appietijdenv2.data.Shift;
import com.mkvld.appietijdenv2.data.Shift.Allowance;
import com.mkvld.appietijdenv2.data.Utils;
import com.mkvld.appietijdenv2.data.WeekShifts;
import com.mkvld.appietijdenv2.databases.HolidayDatabase;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FragExtrasV9 extends PeriodFragV9 {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frag_extras, null, false);
		update(v);
		return v;
	}
	
	protected void update(View root) {
		((TextView)root.findViewById(getTitleViewID())).setText(getResources().getString(R.string.fe_title, getPeriod() + 1, getYear()));
		setHolidays(root);
		setAllowances(root);
		setBudget(root);
	}

	@Override
	public void update() {
		if (getView() != null)
			update(getView());		
	}
	
	protected void setBudget(View root) {
//		List<WeekShifts> periodShifts = Utils.db.getPeriodShifts(getYear(), getPeriod());
		
		if (getTitleViewID() == R.id.fe_title) {
			((TextView)root.findViewById(R.id.fe_contract)).setText(getResources().getString(R.string.fe_budget_above_contract, 0f));
		}
	}
	
	protected void setHolidays(View root) {
		HolidayDatabase db = new HolidayDatabase(getActivity());
		List<Holiday> holidays = db.getHolidays(getYear(), getPeriod());
		LinearLayout holidaysView = (LinearLayout)root.findViewById(getHolidaysViewGroupId());
		holidaysView.removeAllViews();
		if (holidays.size() == 0) {
			TextView tv = new TextView(getActivity());
			tv.setText(R.string.fe_no_holidays);
			tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
			holidaysView.addView(tv);
		} else {			
			for (Holiday holiday : holidays) {
				double allowance = ShiftDatabase.db.averageInLast12Weeks(holiday.getYear(), holiday.getWeek(), holiday.getDay());
				TextView tv = new TextView(getActivity());
				tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
				String dayName = getResources().getStringArray(R.array.dayNames)[holiday.getDay()];
				if (allowance > 0)
					tv.setText(getResources().getString(R.string.fe_holiday, holiday.getName(), dayName, holiday.getWeek() + 1, ShiftDatabase.db.averageInLast12Weeks(holiday.getYear(), holiday.getWeek(), holiday.getDay())));
				else 
					tv.setText(getResources().getString(R.string.fe_holiday_no_bd, holiday.getName(), dayName, holiday.getWeek() + 1, dayName));
				holidaysView.addView(tv);
				
			}
		}
	}
	
	protected final void setAllowances(View root) {
		LinearLayout allowancesView = (LinearLayout)root.findViewById(getAllowancesViewGroupId());
		allowancesView.removeAllViews();
		for (WeekShifts weekShifts : ActMainV9.periodShifts) {
			for (DayShifts dayShifts : weekShifts) {
				for (Shift shift : dayShifts) {
					if (shift.getAllowance().getTotal() > 0) {
						Allowance allowance = shift.getAllowance();
						if (allowance.fixedDayAllowance > 0) {
							TextView tv = new TextView(getActivity());
							tv.setText(getResources().getString(R.string.fe_allowance_sunday, shift.getWeek() + 1, Utils.timeToString(allowance.fixedDayAllowance)));
							tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
							allowancesView.addView(tv);
						}
						if (allowance.dayAllowance > 0 && weekShifts.getWorkTime() > 12) {
							TextView tv = new TextView(getActivity());
							String dayName = getResources().getStringArray(R.array.dayNames)[shift.getDay()];
							tv.setText(getResources().getString(R.string.fe_allowance_hours, dayName, shift.getWeek() + 1, Utils.timeToString(allowance.dayAllowance)));
							tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
							allowancesView.addView(tv);
						}
						
					}
				}
			}
		}
		if (allowancesView.getChildCount() == 0) {
			TextView tv = new TextView(getActivity());
			tv.setText(R.string.fe_no_allowance);
			tv.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
			allowancesView.addView(tv);
		}
	}
	
	protected int getTitleViewID() {
		return R.id.fe_title;
	}
	
	protected int getHolidaysViewGroupId() {
		return R.id.fe_holidays;
	}
	
	protected int getAllowancesViewGroupId() {
		return R.id.fe_allowance;
	}

}
